;;; Copyright 2010 Alessio Stalla.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;      http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(in-package :dynaspring-mock)

(defun make-mock-servlet-context
    (&key base-path (resource-loader (default-resource-loader)))
  (jnew "org.springframework.mock.web.MockServletContext" base-path resource-loader))

(defun servlet-context-attribute (ctx attr-name)
  (jcall "getAttribute" ctx attr-name))

(defun (setf servlet-context-attribute) (value ctx attr-name)
  (jcall "setAttribute" ctx attr-name value))

;;TODO...

(defmacro with-mock-servlet-context
    ((var &optional attributes &rest args) &body body)
  `(let ((,var (make-mock-servlet-context ,@args)))
     (progn
       ,@(loop
            :for attr :in attributes
            :collect `(setf (servlet-context-attribute ,var
                                                       (string ,(car attr)))
                            ,(cadr attr))))
     ,@body))

(defun make-mock-servlet-config
    (servlet-context &optional (name ""))
  (jnew "org.springframework.mock.web.MockServletConfig" servlet-context name))

(defun servlet-config-parameter (c name)
  (jcall "getParameter" c name))

(defun (setf servlet-config-parameter) (value c name)
  (jcall "setParameter" c name value))

;;TODO...

(defmacro with-mock-servlet-config
    ((var servlet-context &optional parameters &key (name ""))
     &body body)
  `(let ((,var (make-mock-servlet-config ,servlet-context ,name)))
     (progn
       ,@(loop
            :for param :in parameters
            :collect `(setf (servlet-config-parameter ,var
                                                      (string ,(car param)))
                            (string ,(cadr param)))))
     ,@body))

(defun make-mock-servlet-request
    (&key (servlet-context (make-mock-servlet-context))
     (method "GET") request-uri)
  (jnew "org.springframework.mock.web.MockHttpServletRequest"
        servlet-context method request-uri))

(defun servlet-request-attribute (req attr-name)
  (jcall "getAttribute" req attr-name))

(defun (setf servlet-request-attribute) (value req attr-name)
  (jcall "setAttribute" req attr-name value))

;;TODO...

(defmacro with-mock-servlet-request
    ((var &rest args &key (servlet-context (make-mock-servlet-context))
          (method "GET") request-uri attributes)
     &body body)
  `(let ((,var (make-mock-servlet-request ,@args :allow-other-keys t)))
     (progn
       ,@(loop
            :for attr :in attributes
            :collect `(setf (servlet-request-attribute ,var
                                                       (string ,(car attr)))
                            ,(cadr attr))))
     ,@body))
